Use dfClient.pkg
Use DataDict.pkg
Use dfTable.pkg
Use Windows.pkg
Use File_dlg.Pkg
Use RegAccess.pkg

Use cPoochDataDictionary.dd
Use dfEnRad.pkg

Enum_List 
    Define CI_NameBreedAge
    Define CI_BreedNameAge
    Define CI_AgeBreedName
End_Enum_List

Enum_List
    Define CI_Success
    Define CI_ErrorNoRecords
    Define CI_ErrorInvalidPath
End_Enum_List

DEFERRED_VIEW Activate_oPoochesView FOR ;
;
Object oPoochesView is a dbView
    Property String psMboxMessage "Pooches"
    
    Object oPooch_DD is a cPoochDataDictionary
        Set Field_Validate_msg Field POOCH.AGE to get_AgeValidation
        
        Function Request_Validate Returns Boolean
            Boolean bFail
            Date dToday
            Integer iHr iMin iSec
            Time currTimeShort
            
            Forward Get Request_Validate to bFail
            If (not(bFail)) Begin
                Move (CurrentDateTime()) to currTimeShort
                Sysdate dToday iHr iMin iSec
                If (iHr >= 19) Begin
                    Move True to bFail
                    Send Info_Box ("Data saved after 7 PM is not allowed.\nCurrent time:" * String(currTimeShort)) "Pooches"
                End
            End
            Function_Return bFail
        End_Function
        
        Function AgeValidation Integer iField Integer iAge Returns Integer
            Integer iFail
            String sName
            
            Get Field_Current_Value Field POOCH.NAME to sName
            Move 0 to iFail
            
            If (not(iAge < 20)) Begin
                Move 1 to iFail
                Send Info_Box ("Pooch" * sName * "must be less than the age of 20. (Age " + String(iAge) + ")") "Pooches"
            End
            
            Function_Return iFail
        End_Function
    End_Object
    
    Set Border_Style to Border_Thick
    Set Location to 10 10
    Set Size to 272 468

    Set Main_DD to oPooch_DD
    Set Server to oPooch_DD
    Set Label to "Pooches"

    Object oPooch_Grid is a dbGrid
        Set Main_File to Pooch.File_Number
        Set Size to 166 443
        Set Location to 7 12
        Set peAnchors to anAll
        Set Wrap_State to True
        //this is a test change.

        Begin_Row
            Entry_Item Pooch.Name
            Entry_Item Pooch.Breed
            Entry_Item Pooch.Age
            Entry_Item Pooch.Create_date
            Entry_Item Pooch.Create_time
            Entry_Item Pooch.Change_date
            Entry_Item Pooch.Change_time
        End_Row

        Set Form_Width   item 0 to 62
        Set Header_Label item 0 to "Name"

        Set Form_Width   item 1 to 100
        Set Header_Label item 1 to "Breed"

        Set Form_Width   item 2 to 40
        Set Header_Label item 2 to "Age"

        Set Form_Width   item 3 to 50
        Set Header_Label item 3 to "Create Date"

        Set Form_Width   item 4 to 58
        Set Header_Label item 4 to "Create Time"

        Set Form_Width   item 5 to 60
        Set Header_Label item 5 to "Change Date"

        Set Form_Width   item 6 to 60
        Set Header_Label item 6 to "Change Time"
        Set peResizeColumn to rcAll

    End_Object    // oPooch_Grid

    Object oDeleteBtn is a Button
        Set Label to "Delete All"
        Set Location to 182 123
        Set peAnchors to anBottomLeft

        Procedure OnClick
            Integer iChoice
            
            Move (YesNo_Box("Delete all records?", (psMboxMessage(Self)), MB_DEFBUTTON2)) to iChoice
            
            If (iChoice = MBR_Yes) Send DeleteAll
        End_Procedure // OnClick
    End_Object    // oDeleteBtn

    Object oImportGrp is a Group
        Set Size to 68 211
        Set Location to 197 14
        Set peAnchors to anBottomLeftRight
        Set Label to "Import"

        Object oOpenImport is a OpenDialog
            Set Dialog_Caption to "Select a file"
            Set Filter_String to "CSV File|*.csv"
            Set Initial_Folder to (psDataPath(phoWorkspace(ghoApplication)))
        End_Object    // oOpenImport

        Object oInputPath is a Form
            Set Label to "Input File (.csv)"
            Set Size to 13 141
            Set Location to 19 4
            Set peAnchors to anLeftRight
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to jMode_Top
        End_Object    // oInputPath

        Object oBrowse is a Button
            Set Label to "Browse"
            Set Location to 19 154
            Set peAnchors to anBottomRight

            Procedure OnClick
                Boolean bOk
                String sPath
                
                If ((Value(oRecentlyUsed)) <> "") Set Initial_Folder of oOpenImport to (Value(oRecentlyUsed))
                Get Show_Dialog of oOpenImport to bOk
                
                If (bOk) Begin
                    Move (File_Name(oOpenImport)) to sPath
                    Set Value of oInputPath to sPath
                End
            End_Procedure // OnClick
        End_Object    // oBrowse

        Object oImport_Btn is a Button
            Set Label to "Import"
            Set Location to 43 154
            Set peAnchors to anBottomRight

            Procedure OnClick
                Integer iStatus
                String sPath

                Move (Value(oInputPath)) to sPath
                Get ImportCsvRecords sPath to iStatus
                
                If (iStatus = CI_Success) Begin
                    Send RegistryUpdate
                    Send RegistryFillComboBox
                    Send Beginning_of_Data of oPooch_Grid
                    Send Info_Box "Records have been imported into the database." (psMboxMessage(Self))
                End
                If (iStatus = CI_ErrorInvalidPath) Send Info_Box ("File path is invalid. (" + sPath + ")\nPlease select a file to import and try again.") (psMboxMessage(Self))
                
            End_Procedure // OnClick
        End_Object    // oImport_Btn
        
        Object oRecentlyUsed is a ComboForm
            Set Size to 13 141
            Set Location to 43 4
            Set Label to "Look In"
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to JMode_Top
            Set Entry_State to False
            Set Combo_Sort_State to False
            Set peAnchors to anTopLeftRight
            Set ListWidth to 550
            
            Procedure Combo_Fill_List
                Send RegistryFillComboBox
            End_Procedure
            
        End_Object //oRecentlyUsed

    End_Object    // oImportGrp

    Object oReportGrp is a Group
        Set Size to 75 210
        Set Location to 185 242
        Set peAnchors to anBottomRight
        Set Label to "Report"
        
        Object oOutputFile is a Form
            Set Label to "Output File Name"
            Set Size to 13 85
            Set Location to 18 109
            Set peAnchors to anBottomRight
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to jMode_Top
        End_Object    // oOutputFile

        Object oTextBox1 is a TextBox
            Set Size to 10 50
            Set Location to 32 111
            Set Label to '(Folder same as input file.)'
        End_Object

        Object oReport is a Button
            Set Label to "Report"
            Set Location to 52 142
            Set peAnchors to anBottomRight

            Procedure OnClick
                String sFolderPath sFileName
                Integer iSortBy iStatus
                String[] asPooches
                
                Move (Current_Radio(oReportSortBy)) to iSortBy
                Move (Value(oInputPath)) to sFolderPath
                Move (Left(sFolderPath, (RightPos("\", sFolderPath)-1))) to sFolderPath //truncate trailing backslash
                Move (Value(oOutputFile)) to sFileName
                
                Get LoadDbRecords iSortBy to asPooches
                Get WriteToFile sFolderPath sFileName asPooches to iStatus
                
                If (iStatus = CI_Success) Send Info_Box "Your report has been generated." (psMboxMessage(Self))
                If (iStatus = CI_ErrorNoRecords) Send Info_Box "There are no records available to generate a report. Report aborted." (psMboxMessage(Self))
                If (iStatus = CI_ErrorInvalidPath) Send Info_Box "Invalid folder or file path. Report aborted." (psMboxMessage(Self))
      
            End_Procedure // OnClick
        End_Object    // oReport

        Object oReportSortBy is a RadioGroup
            Set Location to 11 9
            Set Size to 54 81
            Set Label to "Sort by"
        
            Object oNameBreedAge is a Radio
                Set Label to "Name Breed Age"
                Set Size to 10 70
                Set Location to 10 5
            End_Object
        
            Object oBreedNameAge is a Radio
                Set Label to "Breed Name Age"
                Set Size to 10 70
                Set Location to 25 5
            End_Object
        
            Object oAgeBreedName is a Radio
                Set Label to "Age Breed Name"
                Set Size to 10 70
                Set Location to 40 5
            End_Object
            
        End_Object

    End_Object    // oReportGrp
    
    Procedure DeleteAll
        Boolean bFound
        
        Clear POOCH
        Repeat
            Find GT POOCH by Recnum
            Move Found to bFound
            If (bFound) Begin
                Reread POOCH
                    Delete POOCH
                Unlock
            End
        Until (not(bFound))
        Send Beginning_of_Data of oPooch_Grid
    End_Procedure // DeleteAll
    
    Function FormatRecordString String sName String sBreed String sAge Integer iSortBy Returns String
        String sFullRecord
        
        If (Integer(sAge) < 10) Move (" " + sAge) to sAge
        
        Case Begin
            Case (iSortBy = CI_NameBreedAge)
                Move (Pad(sName,30) + Pad(sBreed,30) + Pad(sAge,30)) to sFullRecord
                Case Break
            Case (iSortBy = CI_BreedNameAge)
                Move (Pad(sBreed,30) + Pad(sName,30) + Pad(sAge,30)) to sFullRecord
                Case Break
            Case (iSortBy = CI_AgeBreedName)
                Move (Pad(sAge,30) + Pad(sBreed,30) + Pad(sName,30)) to sFullRecord
                Case Break
        Case End
        
        Function_Return sFullRecord
    End_Function // FormatRecordString
    
    Function ImportCsvRecords String sPath Returns Integer
        Integer iChannel iStatus
        String sName sBreed sAge
        Boolean bFail
        
        Move CI_Success to iStatus
        
        Get Seq_New_Channel to iChannel
        Direct_Input channel iChannel sPath
        If (sPath <> "") Begin
            If (not(SeqEof)) Begin
                Repeat
                    Readln channel iChannel sName sBreed sAge

                    Send Clear of oPooch_DD
                    Move sName to POOCH.NAME
                    Find EQ POOCH by Index.1
                    If (not(Found) and sName <> "") Begin
                        Set Field_Changed_Value of oPooch_DD Field POOCH.NAME to sName
                        Set Field_Changed_Value of oPooch_DD Field POOCH.BREED to sBreed
                        Set Field_Changed_Value of oPooch_DD Field POOCH.AGE to sAge
                        
                        Get Request_Validate of oPooch_DD to bFail
                        If (not(bFail)) Send Request_Save of oPooch_DD
                    End
                Until (SeqEof)
            End
            Else Move CI_ErrorInvalidPath to iStatus
        End
        Else Move CI_ErrorInvalidPath to iStatus
        
        Close_Input channel iChannel
        Send Seq_Release_Channel iChannel
        
        Function_Return iStatus
    End_Function // ImportCsvRecords
    
    Function LoadDbRecords Integer iSortBy Returns String[]
        Boolean bFound
        String[] asPooches
        String sName sBreed sAge
        Integer i
        
        Move 0 to i
        
        Clear POOCH
        Repeat
            Find GT POOCH by Recnum
            Move Found to bFound
            If (bFound) Begin
                Move (POOCH.NAME)   to sName
                Move (POOCH.BREED)  to sBreed
                Move (POOCH.AGE)    to sAge
                Get FormatRecordString sName sBreed sAge iSortBy to asPooches[i]
                Increment i
            End
        Until (not(bFound))
        
        Move (SortArray(asPooches)) to asPooches
        
        Function_Return asPooches
    End_Function // LoadDbRecords
    
    Function WriteToFile String sFolderPath String sFileName String[] asPooches Returns Integer
        Integer iChannel iSize i iStatus
        String sPath
        
        Move (SizeOfArray(asPooches) - 1) to iSize
        Move (sFolderPath + "\" + sFileName) to sPath
        Move CI_Success to iStatus
        
        Get Seq_New_Channel to iChannel
        Direct_Output channel iChannel sPath
        
        If (iSize <> -1) Begin
            If ((sFolderPath <> "") and (sFileName <> "")) Begin
                If (not(SeqEof)) Begin
                    For i from 0 to iSize
                        Writeln channel iChannel asPooches[i]
                    Loop
                End
                Else Move CI_ErrorInvalidPath to iStatus
            End
            Else Move CI_ErrorInvalidPath to iStatus
        End
        Else Move CI_ErrorNoRecords to iStatus
        
        Close_Output channel iChannel
        Send Seq_Release_Channel iChannel
        
        Function_Return iStatus
    End_Procedure //WriteToFile
    
    Procedure RegistryFillComboBox
        String[] sPaths
        String sTopValue
        Integer iSize i
        
        Send BuildRegistrySubkey of oRecentFolderReg 
        Get ReturnFolderPathArray of oRecentFolderReg to sPaths
        Send Combo_Delete_Data of oRecentlyUsed
        Move (SizeOfArray(sPaths) - 1) to iSize
        
        For i from 0 to iSize
            Send Combo_Add_Item of oRecentlyUsed sPaths[i]
        Loop
        
        If (iSize > -1) Begin 
            Get Combo_Value of oRecentlyUsed item 0 to sTopValue
            Set Value of oRecentlyUsed to sTopValue 
        End
        
    End_Procedure
    
    Procedure RegistryUpdate
        String sFolderPath
        
        Get Value of oInputPath to sFolderPath
        Move (Left(sFolderPath, (RightPos("\", sFolderPath) - 1))) to sFolderPath

        Send BuildRegistrySubkey of oRecentFolderReg
        If (sFolderPath <> "") Send RestackFolders of oRecentFolderReg sFolderPath
    End_Procedure
    
CD_End_Object    // oPoochesView
